package com.bmslz.monitor.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bmslz.monitor.base.util.StringUtils;
import com.bmslz.monitor.base.entity.MonitorModel;
import com.bmslz.monitor.mapper.MonitorModelMapper;
import com.bmslz.monitor.service.IMonitorModelService;
import com.bmslz.monitor.base.vo.MonitorAttVo;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Description: 监控模型
 * @Author: bobo
 * @Date: 2025-08-03
 * @Version: V1.0
 */
@Service
public class MonitorModelServiceImpl extends ServiceImpl<MonitorModelMapper, MonitorModel> implements IMonitorModelService {

    @Override
    public List<String> tagList() {
        return baseMapper.tagList();
    }

    @Override
    public MonitorModel queryByNo(String modelNo) {
        return this.getOne(new LambdaQueryWrapper<MonitorModel>().eq(MonitorModel::getNumber, modelNo), false);
    }

    @Override
    public String keyConvertName(MonitorModel monitorModel, String key) {
        String att = monitorModel.getAtt();
        if (StringUtils.isEmpty(att)) {
            return "";
        }
        try {
            List<MonitorAttVo.Item> attList = MonitorAttVo.parseArrayJson(att);
            List<MonitorAttVo.Item> filtered = attList.stream().filter(item -> item.getAttKey().equals(key)).collect(Collectors.toList());
            if (filtered.size() > 0) {
                return filtered.get(0).getAttName();
            } else {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    public Long total(Date startTime, Date endTime) {
        LambdaQueryWrapper<MonitorModel> wrapper = new LambdaQueryWrapper<MonitorModel>();
        if (startTime != null) {
            wrapper.ge(MonitorModel::getCreateTime, startTime);
        }
        if (endTime != null) {
            wrapper.le(MonitorModel::getCreateTime, endTime);
        }
        return this.count(wrapper);
    }
}
